//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "client\fn_clientExecuteDrop.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_object","_action","_parameters","_player","_action_id","_arguments","_item_desc","_object_array","_pickedup_item","_obj_type","_creation_pos","_new_object","_player_objects","_array_index"
		,"_forindex","_object_index","_pickedup_objects","_action_display_timer"];
DEFAULT_PARAM(_object,TMF_EXECUTE_ACTION_OBJECT,objNull);
DEFAULT_PARAM(_caller,TMF_EXECUTE_ACTION_CALLER,objNull);
DEFAULT_PARAM(_act_id,TMF_EXECUTE_ACTION_ID,-1);
DEFAULT_PARAM(_arguments,TMF_EXECUTE_ACTION_ARGS,[]);
DEFAULT_PARAM(_action,TMF_EXECUTE_ACTION_ACTION,[]);

_action_id = 	GETVAL(_arguments,TMF_EXECUTE_ACTION_PARAM_ACTID,-1);
_object = 		GETVAL(_arguments,TMF_EXECUTE_ACTION_PARAM_TARGET,objNull);

if (!isnull _object) then {
	if (_action_id > 0) then {
		_instance_id = [_object] call FFUNC(getInstanceID);
		if (count _action  <= 0) then {
			_action = [_object,_action_id] call FFUNC(actionGet);
		};
		TRACE_1("Action",_action);
		_parameters = [_action] call FFUNC(actionGetParams);
			
		_act_id = [_object,_action,"DROP_ACTION",-1] call FFUNC(actionGetLocal);
		if (_act_id >= 0) then {
			TRACE_1("Remove Drop Action",_act_id);
			player removeAction _act_id;
		};
		[_object,_action,"DROP_ACTION",-1] call FFUNC(actionSetLocal);

		_item_desc = [_parameters, "ITEM_DESC", typeof(_object),THIS_FILE] call FFUNC(hashGet);
		_pickup_msg = [_parameters, "PICKUP_MSG", [],THIS_FILE] call FFUNC(hashGet);
		_shared_variable = [_parameters, "SHARED_VAR", "",THIS_FILE] call FFUNC(hashGet);
		_initial_status = false; //[_parameters, "SHARED_VAR_VAL", false,THIS_FILE] call FFUNC(hashGet);
		_action_title = [_parameters, "PICKUP_TITLE", "STR_TMF_PICKUP_DEFACTION",THIS_FILE] call FFUNC(hashGet);
		_pickup_code = [_parameters, "PICKUP_CODE", "",THIS_FILE] call FFUNC(hashGet);
		_shared_variable = _shared_variable call FFUNC(getStringVar);
		
		_act_id = [_object,_action,"PICKUP_ACTION",-1] call FFUNC(actionGetLocal);
		if (_act_id < 0) then {
			if (_action_title != "") then {
				_obj_var_picked = format["mission_object_pickedup_%1",_action_id];
				_action_title = (_action_title) call FFUNC(translate);
				_arguments = [];
				_arguments set [TMF_EXECUTE_ACTION_PARAM_ACTID, _action_id];
				_action_display_timer = QGVAR(action_display_timer);
				_object setVariable [_action_display_timer,0];
				if (_initial_status) then {
					_condition = Format["(alive _target) && {vehicle player == player} && {(_target getVariable '%1')} && {(_target getVariable '%2' < time)}",_obj_var_picked,_action_display_timer];
				} else {
					_condition = Format["(alive _target) && {vehicle player == player} && {!(_target getVariable '%1')} && {(_target getVariable '%2' < time)}",_obj_var_picked,_action_display_timer];
				};
				_act_id = _object addAction [_action_title, QPATH(addAction.sqf),_arguments,10,True,True,"",_condition];
				// We set the variable as local because it is dependent on the computer running it.
				[_object,_action,"PICKUP_ACTION",_act_id] call FFUNC(actionSetLocal);
				TRACE_1("Added Action",ARR_4(_act_id,_action_title,typeof _object,_condition));
			};
		};

		_object hideObject false;
		TRACE_1("Object now shown",ARR_2(typeof _object, _action_id));
	};
};
TRACE("End");
TMFPROFILERSTOP;